package com.proposition.web.exam;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.proposition.commons.exam.exception.ExamAddException;
import com.proposition.commons.exam.exception.ExamException;
import com.proposition.commons.exam.result.Result;
import com.proposition.commons.exam.result.ResultEnum;
import com.proposition.commons.exam.result.ResultUtil;
import com.proposition.commons.exam.util.ExamUtil;
import com.proposition.commons.model.*;
import com.proposition.exam.service.*;
import com.proposition.support.annotation.LogAnnotation;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 8:47 2017/11/1
 * @description:
 */
@Controller
@RequestMapping("/exam/examAdd")
public class ExamAddController {
    @Autowired
    private ExamAcupointService examAcupointService;
    @Autowired
    private ExamAnswer10Service examAnswer10Service;
    @Autowired
    private ExamAcupointPediatricService examAcupointPediatricService;
    @Autowired
    private ExamAcupointAdultService examAcupointAdultService;
    @Autowired
    private ExamAnswer30Service examAnswer30Service;

    /**
     * @author: 陈玉林
     * @date: 2017/11/1 9:49
     * @description: 进入自主命题界面
     */
    @RequiresPermissions("zzmt")
    @GetMapping("/page")
    public String intoExamAddPage(Model model) {
        model.addAttribute("partList", new ExamPart().selectList(new EntityWrapper()));
        model.addAttribute("cognitiveLevelList", new ExamCognitiveLevel().selectList(new EntityWrapper()));
        model.addAttribute("singlePloyFiveCategoryList", new ExamSinglePloyFiveCategory().selectList(new EntityWrapper()));
        model.addAttribute("singlePloyPediatricSubclassIdList", new ExamCategorySinglePloyPediatricSubclass().selectList(new EntityWrapper()));
        ExamUtil.ensureExamParamIsNotNull();

        model.addAttribute("examParamWendaAcupoint",new ExamParamWendaAcupoint().selectOne(new EntityWrapper()));
        model.addAttribute("examParamWendaFSENC", new ExamParamWendaFSENC().selectOne(new EntityWrapper()));
        model.addAttribute("examParamAcupointLocationAdult",  new ExamParamAcupointLocationAdult().selectOne(new EntityWrapper()));
        model.addAttribute("examParamAcupointLocationPediatric", new ExamParamAcupointLocationPediatric().selectOne(new EntityWrapper()));
        model.addAttribute("examParamSinglePloyCEC",new ExamParamSinglePloyCEC().selectOne(new EntityWrapper()));
        model.addAttribute("examParamCompletePloyFSEN", new ExamParamCompletePloyFSEN().selectOne(new EntityWrapper()));
        return "exam/exam-add";
    }

    /**
     * @author: 陈玉林
     * @date: 2017/11/2 9:21
     * @description:/1 1代表试题分类
     *
     * 问答题-腧穴
     */
    @LogAnnotation(moduleName = "试题管理", operate = "自主命题")
    @PostMapping(value = "/1")
    @ResponseBody
    public Result add(@RequestParam String answerWD[],@RequestParam String answerWD1[],@RequestParam Float answerWDScore[],@RequestParam Float answerWDScore1[], ExamAcupoint exam, ExamAcupointOriginal examOriginal) {
        try {
            return examAcupointService.add(answerWD,answerWDScore,answerWD1,answerWDScore1,exam, examOriginal);
        }catch (AuthorizationException e) {
            e.printStackTrace();
            return new Result(ResultEnum.ILLEGAL_AUTHORITY,null);
        }  catch (ExamAddException e) {
            e.printStackTrace();
            return e.getResult();
        } catch (ExamException e1) {
            e1.printStackTrace();
            return e1.getResult();
        } catch (Exception e2) {
            e2.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    /**
     * @author: 陈玉林
     * @date: 2017/11/3 10:22
     * @description: 问答题-内科、伤科、妇科、儿科、触诊
     * 单项手法-儿科手法、成人手法、触诊诊断
     *
     */
    @LogAnnotation(moduleName = "试题管理", operate = "自主命题")
    @ResponseBody
    @RequestMapping(value = {"/23456","/9-10-11"},method = RequestMethod.POST)
    public Result add(
            @RequestParam(required = false) String answerWD2[],@RequestParam(required = false) Float answerWDScore2[],
            @RequestParam(required = false) String answerDXSF[],@RequestParam(required = false) Float answerDXSFScore[],
            ExamAnswer10 exam,ExamAnswer10Original examOriginal) {
        try {
            return examAnswer10Service.add(answerWD2,answerWDScore2,answerDXSF,answerDXSFScore,exam,examOriginal);
        } catch (ExamAddException e) {
            e.printStackTrace();
            return e.getResult();
        } catch (ExamException e1) {
            e1.printStackTrace();
            return e1.getResult();
        } catch (Exception e2) {
            e2.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    /**
     * @author: 陈玉林
     * @date: 2017/11/3 15:40
     * @description: 穴位定位-成人穴位
     */
    @LogAnnotation(moduleName = "试题管理", operate = "自主命题")
    @ResponseBody
    @PostMapping(value = "/8")
    public Result add(@RequestParam String answerXWDW[],@RequestParam Float answerXWDWScore[],ExamAcupointAdult exam,ExamAcupointAdultOriginal examOriginal) {
        try {
            return examAcupointAdultService.add(answerXWDW,answerXWDWScore,exam,examOriginal);
        } catch (ExamAddException e) {
            e.printStackTrace();
            return e.getResult();
        } catch (ExamException e1) {
            e1.printStackTrace();
            return e1.getResult();
        } catch (Exception e2) {
            e2.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    /**
     * @author: 陈玉林
     * @date: 2017/11/3 15:40
     * @description: 穴位定位-儿科穴位
     */
    @LogAnnotation(moduleName = "试题管理", operate = "自主命题")
    @ResponseBody
    @PostMapping(value = "/7")
    public Result add(@RequestParam String answerXWDW1[],@RequestParam Float answerXWDWScore1[],ExamAcupointPediatric exam,ExamAcupointPediatricOriginal examOriginal) {
        try {
            return examAcupointPediatricService.add(answerXWDW1,answerXWDWScore1,exam,examOriginal);
        }  catch (ExamAddException e) {
            e.printStackTrace();
            return e.getResult();
        } catch (ExamException e1) {
            e1.printStackTrace();
            return e1.getResult();
        } catch (Exception e2) {
            e2.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    /**
     * @author: 陈玉林
     * @date: 2017/11/3 15:40
     * @description: 成套手法-妇、伤、儿、内科
     */
    @LogAnnotation(moduleName = "试题管理", operate = "自主命题")
    @ResponseBody
    @PostMapping(value = "/12-13-14-15")
    public Result add(@RequestParam String answerCTSF[],@RequestParam Float answerCTSFScore[],ExamAnswer30 exam,ExamAnswer30Original examOriginal) {
        try {
            return examAnswer30Service.add(answerCTSF,answerCTSFScore,exam,examOriginal);
        } catch (ExamAddException e) {
            e.printStackTrace();
            return e.getResult();
        } catch (ExamException e1) {
            e1.printStackTrace();
            return e1.getResult();
        } catch (Exception e2) {
            e2.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }
}
